<template>
	<view class="box screen-all flex center">
		<image class="logo-icon" src="../../static/home_bg.png" mode="aspectFit"></image>
		<text class="logo-title">派对兔</text>
		
		<view class="content-view flex">
			<view class="input-view flex row aCenter">
				<input class="flex-1 input-text" v-model="account" placeholder="输入管理账号"/>
			</view>
			<view class="input-view flex row aCenter">
				<input class="flex-1 input-text" v-model="password" :password="true" placeholder="输入管理密码"/>
			</view>
			
			<view class="btn-view flex center" @click="wxLogin()">
				登录
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: '',
				password: '',
			}
		},
		onLoad() {
			this.account = uni.getStorageSync("adminaccount");
			this.password = uni.getStorageSync("adminpassword");
		},
		methods: {
			wxLogin () {
				// uni.setStorageSync("wechat_code", code);
				let wechat_code = uni.getStorageSync("wechat_code");
				
				if(!this.account){uni.showToast({title:"请输入管理账号",icon:"none"}); return}
				if(!this.password){uni.showToast({title:"请输入管理密码",icon:"none"}); return}
				
				uni.showLoading({
					title: "登录中..."
				})
				
				uni.setStorageSync("adminaccount", this.account);
				uni.setStorageSync("adminpassword", this.password);
				
				let params = {
					code: wechat_code,
				}
				
				console.log(params);
				this.$request("api/user/get_openid", params)
					.then(({ code, msg, data, time }) => {
						console.log(code, msg, data, time)
						if(code == 1){
							this.login(data);
						}else{
							uni.hideLoading();
							uni.showToast({
								title: msg, icon: 'none',
							})
						}
					}).catch((e) => {
						uni.hideLoading();
						console.log(e);
					});
			},
			login (obj) {
				let params = {
					account: this.account,
					password: this.password,
					nickname: obj.nickname,
					avatar: obj.avatar,
					openid: obj.openid,
				}
				
				console.log(params);
				this.$request("api/user/login", params)
					.then(({ code, msg, data, time }) => {
						uni.hideLoading();
						console.log(code, msg, data, time)
						uni.showToast({
							title: msg, icon: 'none',
						})
						
						if(code == 1){
							// 缓存token
							uni.setStorageSync("token", data.token);
							// 缓存用户身份
							uni.setStorageSync("role", data.role);
							
							uni.reLaunch({
								url: "../role-admin/tabbar-home"
							})
						}else{
							
						}
					}).catch((e) => {
						uni.hideLoading();
						console.log(e);
					});
			}
		}
	}
</script>

<style scoped>
	.box {
		background-color: #010B24;
	}
	.logo-icon {
		width: 207rpx;
		height: 207rpx;
		margin-top: -150rpx;
		margin-bottom: 26rpx;
	}
	.logo-title {
		font-size: 32rpx;
		color: #FFF;
		font-weight: bold;
	}
	.content-view {
		width: 670rpx;
		background-color: #121C33;
		border-radius: 20rpx;
		margin-top: 38rpx;
	}
	.input-view {
		height: 100rpx;
		margin: 0 37rpx;
		border-bottom-width: 1rpx;
		border-bottom-color: #373F50;
		border-bottom-style: solid;
		margin-top: 30rpx;
	}
	.input-text {
		font-size: 28rpx;
		color: #FFF;
		font-weight: bold;
	}
	.btn-view {
		width: 592rpx;
		height: 92rpx;
		margin-top: 100rpx;
		margin-bottom: 20rpx;
		background-image: linear-gradient(to bottom, #D57FF5, #BA45E6);
		border-radius: 50rpx;
		margin-left: 43rpx;
		font-size: 30rpx;
		color: #FFF;
	}
	.btn-view:active {
		background-image: linear-gradient(to bottom, #D57FF5DD, #BA45E6DD);
	}
	.sms-view {
		padding: 20rpx 30rpx;
		font-size: 28rpx;
		color: #FFFFFF;
	}
	.sms-view:active {
		color: #FFFFFFDD;
	}
</style>
